﻿using Jhecis.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace Jhecis.Auto
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        #region 异步
        Dictionary<string, DateTime> StateCaseId = new Dictionary<string, DateTime>();
        private Thread ThMQ = null;
        bool IsContinue = true;
        #endregion

        #region 回调
        delegate void Mydate();
        public void ClearCache()
        {
            foreach (var item in StateCaseId.Keys)
            {
                DateTime dt = StateCaseId[item];
                TimeSpan span = DateTime.Now - dt;
                if (span.Hours > 24)
                {  //避免跑的时间太长，满了
                    StateCaseId.Remove(item);
                }
            }
        }
        #endregion
        BingLiBLL bll = new BingLiBLL();
        SendHelper SendHelper = new SendHelper();
        public void ThMethod22()
        {
            while (IsContinue)
            {
                if (DateTime.Now.Hour == 1)
                {
                    string Datetime = DateTime.Now.ToString("yyyy-MM-dd");
                    if (!StateCaseId.ContainsKey(Datetime))
                    {
                        #region 发送
                        try
                        {
                            #region 发送前一天的数据
                            string start = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                            string end = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                            DataTable DtAllPatient = bll.GetAllPatients(start, end);
                            if (DtAllPatient != null && DtAllPatient.Rows.Count > 0)
                            {
                                for (int i = 0; i < DtAllPatient.Rows.Count; i++)
                                {
                                    string pvid = DtAllPatient.Rows[i]["PATIENT_ID"].ToString();
                                    SendHelper.SendXmlByPvid(pvid);
                                }
                            }
                            #endregion

                            #region 发送完，标记已发送
                            if (!StateCaseId.ContainsKey(Datetime))
                            {
                                StateCaseId.Add(Datetime, DateTime.Now);
                            }
                            #endregion

                            #region 发送完毕，异步，清理缓存
                            Mydate callBack = new Mydate(ClearCache);
                            callBack.BeginInvoke(null, null);
                            LogHelper.Instance.WriteInfo("清空缓存..");
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Instance.WriteError("定时任务报错：" + ex.Message);
                        }
                        #endregion
                    }
                }
                Thread.Sleep(1000 * 60 * 10);
            }
        }
        protected override void OnStart(string[] args)
        {
            LogHelper.Instance.WriteInfo("服务启动");
            ThMQ = new Thread(ThMethod22);
            ThMQ.IsBackground = true;
            ThMQ.Start();
        }

        protected override void OnStop()
        {
            LogHelper.Instance.WriteInfo("服务关闭");
            IsContinue = false;
            if (ThMQ != null)
            {
                ThMQ.Abort();
            }
        }
    }
}
